iload = true;
var iloadnow = 0;
function loading(){
	document.getElementById('loading').innerHTML = 'Loading..<span id="iload"></span>';
	iloading();
}
function iloading(){
	iload = true;
	if(iload){
		iloadnow ++;
		if(iloadnow > 3)
			iloadnow = 0;
		//var icon = new Array('\\','-','/','|');
		var icon = new Array('.','..','...','....');
		document.getElementById('iload').innerHTML = icon[iloadnow];
		setTimeout('iloading();',160);
	}else{
		document.getElementById('iload').innerHTML = '';
	}
}
function upload_watermark(is_watermark){
	document.getElementById('flash_upload').innerHTML = '<embed type="application/x-shockwave-flash" src="upload.swf' +(is_watermark ? '?watermark='+is_watermark : '?watermark=no') + '" quality="high" wmode="transparent" pluginurl="http://www.macromedia.com/go/getflashplayer" pluginspage="http://www.macromedia.com/go/getflashplayer" width="75" height="25"></embed>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<iframe src="index.php?delete_img='+is_watermark+'" width="250" height="30" scrolling="no" frameborder="0""></iframe>';
}
function responseStatus(msg){
	document.getElementById('loading').innerHTML = msg;
	if(msg == 'Done!'){
		iload = false;	
		document.getElementById('getcode').style.display = 'block';
		setTimeout("showcode('bbcode');",0000);
	}
}
function clearlist(){
   document.getElementById('result').innerHTML = "";
   document.getElementById('loading').innerHTML = "";
}
function showcode(type){
	var code = new Array();
	if(type == 'html'){		
		code[0] = '&lt;img src="';
		code[1]	= '"&gt;';
	}else if(type == 'bbcode'){
		code[0] = '[IMG]';
		code[1]	= '[/IMG]';
	}	
	else{
		code[0] = '';
		code[1] = '';
	}
	var content = document.getElementById('result').innerHTML;
	var html = '';
	var re = /<div>.*?(http:.*?)\.(jpg|png|bmp|gif|jpeg)<\/div>/ig;
	var m = re.exec(content);
	while (m) {
		html += code[0] + m[1] + '.' + m[2] + code[1] + "\r\n";
		m = re.exec(content);	
	}	
	document.getElementById('showcode').innerHTML = html;
}
var transfer_id = 0;
function transfer(id){	
	transfer_id = (!id) ? transfer_id : id;
	var is_watermark = document.getElementById('watermark').checked ? 'yes' : 'no';
	iload = true;
	loading();
	text = document.getElementById('listurl').value;
	var re = /.*?(https?:.*?)\.(jpg|png|bmp|gif|jpeg)/ig;
	var m = re.exec(text);
	var html = "";
	while (m) {
		html += m[1] + '.' + m[2]+ "\n";
		m = re.exec(text);	
	}	
	ex = html.split("\n");
	url = ex[transfer_id].replace("\n", "");	
	transferUrl(url, transfer_id, ex.length, is_watermark);	
	
}
function transferUrl(url, transfer_id, count, is_watermark){
	var ajax = new AJAX_Handler();
	ajax.onreadystatechange(check);
	transfer_id += 1;
	ajax.send('upload.php', 'url='+encodeURI(url)+'&watermark='+is_watermark+'&sid='+ Math.random());
	function check(){
		if(ajax.xmlHttp.readyState == 4 && ajax.xmlHttp.status == 200){
			iload = false;
			dv = document.createElement("div");
			dv.innerHTML = transfer_id + ' - ' + ajax.xmlHttp.responseText;
			if(transfer_id == count - 1)
				responseStatus("Done!");
			else
				transfer(transfer_id);
			document.getElementById("result").appendChild(dv);
			
		}
	}
}

function displaypic(name, url){
	dv = document.createElement("div");
	//dv.innerHTML = name+' - '+url;
	dv.innerHTML = '- '+name;
	document.getElementById('result').appendChild(dv);
}

var AJAX_Handler=function()
{this.xmlHttp=false;try{this.xmlHttp=new XMLHttpRequest();}catch(e){try{this.xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){try{this.xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){alert('Your browser does not support AJAX');return;}}}
this.onreadystatechange=function(updateFunc){this.updateFunc=updateFunc;}
this.send=function(url,param){param=param?param:"";this.xmlHttp.onreadystatechange=this.updateFunc;this.xmlHttp.open("POST",url,true);this.xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");this.xmlHttp.setRequestHeader("Content-length",param.length);this.xmlHttp.setRequestHeader("Connection","close");this.xmlHttp.send(encodeURI(param));}}

